<?php
class Model_User extends CI_Model
{
	
	function get_specific_all($num, $offset, $sort, $keyarray)
	{
		$this->db->from('user u');
		
		if($sort)
		{
			$this->db->order_by($sort);
		}
		else
		{
			$this->db->order_by('u.user_id', 'ASC');
		}
		
		if ($offset < 0) {
			$offset = 0;
		}				

		if ($num < 1) {
			$num = 20;
		}	
		
		$this->db->limit((int)$num, (int)$offset);
		
		return $this->db->get()->result_array();
	}
	
	
	function get_specific_count_all($keyarray)
	{
		$this->db->select('COUNT(*) AS total');
		$this->db->from('user u');
		
		return $this->db->get()->row()->total;
	}
	
	
	function login($username, $password)
	{
		$query = $this->db->get_where('user', array('username' => $username,'password'=>md5($password)), 1);
		if ($query->num_rows() ==1)
		{
			$row=$query->row();
			$this->session->set_userdata('user_id', $row->user_id);
			$this->session->set_userdata('username', $username);
			
			//echo $row->user_id."<br>";
			//echo $username."<br>";
			//exit;
			
			
			return true;
		}
		return false;
	}
	
	
	function logout()
	{
		$this->session->sess_destroy();
		redirect('user/login');
	}
	
	
	function is_logged_in()
	{
		return $this->session->userdata('user_id')!=false;
	}
	
	
	function get_logged_in_user_info()
	{
		if($this->is_logged_in())
		{
			return $this->get_info($this->session->userdata('user_id'));
		}
		
		return false;
	}
	
	function has_permission($module_id,$user_id)
	{
		
		if($module_id==null)
		{
			return true;
		}
		
		$query = $this->db->get_where('user_permission', array('user_id' => $user_id,'module_id'=>$module_id), 1);
		return $query->num_rows() == 1;
		
		
		return false;
	}
	
	
	function exists($user_id)
	{
		$this->db->from('user');	
		
		$this->db->where('user_id',$user_id);
		$query = $this->db->get();
		
		return ($query->num_rows()==1);
	}	
	
	
	function username_exists($username, $user_id)
	{
		$this->db->from('user');	
		
		$this->db->where('username',$username);
		if($user_id > 0)
		{
			$this->db->where('user_id != ', $user_id);
		}
		$query = $this->db->get();
		
		return ($query->num_rows()==1);
	}		
	
	function get_all($numPerPage, $pageNum)
	{
		$this->db->from('user');	
		
		$this->db->order_by("user_id", "asc");
		$this->db->limit($numPerPage, ($pageNum - 1)*$numPerPage);
		return $this->db->get();		
	}
	
	function get_total_num()
	{
		$this->db->from('user');	
		
		
		return $this->db->count_all_results();	
		
	}
	
	
	function get_info($user_id)
	{
		$this->db->from('user');	
		
		$this->db->where('user_id',$user_id);
		$query = $this->db->get();
		
		
		return $query->row();
		
		
	}
	
	
	public function addUser($data) {
		$this->db->query("INSERT INTO `" . $this->db->dbprefix . "user` SET username = " . $this->db->escape($data['username']) . ", password = " . $this->db->escape(md5($data['password'])) . ", firstname = " . $this->db->escape($data['firstname']) . ", lastname = " . $this->db->escape($data['lastname']) . ", email = " . $this->db->escape($data['email']) . ", user_group_id = '" . (int)$data['user_group_id'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
		
		$user_id = $this->db->insert_id();
		
		//复制所有权限给新user
		$this->db->select('DISTINCT(module_id)');
		$this->db->from('user_permission');
		$results = $this->db->get()->result_array();
		foreach($results as $result)
		{
			$this->db->query("INSERT INTO `" . $this->db->dbprefix . "user_permission` SET module_id = " . $this->db->escape($result['module_id']) . ", user_id = '" . (int)$user_id . "'");
		}
	}
	
	public function editUser($user_id, $data) {
		$this->db->query("UPDATE `" . $this->db->dbprefix . "user` SET username = " . $this->db->escape($data['username']) . ", firstname = " . $this->db->escape($data['firstname']) . ", lastname = " . $this->db->escape($data['lastname']) . ", email = " . $this->db->escape($data['email']) . ", user_group_id = '" . (int)$data['user_group_id'] . "', status = '" . (int)$data['status'] . "' WHERE user_id = '" . (int)$user_id . "'");
		
		if ($data['password']) {
			$this->db->query("UPDATE `" . $this->db->dbprefix . "user` SET password = " . $this->db->escape(md5($data['password'])) . " WHERE user_id = '" . (int)$user_id . "'");
		}
	}

	public function editPassword($user_id, $password) {
		$this->db->query("UPDATE `" . $this->db->dbprefix . "user` SET password = " . $this->db->escape(md5($password)) . " WHERE user_id = '" . (int)$user_id . "'");
	}

	public function editCode($email, $code) {
		$this->db->query("UPDATE `" . $this->db->dbprefix . "user` SET code = " . $this->db->escape($code) . " WHERE email = " . $this->db->escape($email) . "");
	}
			
	public function deleteUser($user_id) {
		$this->db->query("DELETE FROM `" . $this->db->dbprefix . "user` WHERE user_id = '" . (int)$user_id . "'");
		$this->db->query("DELETE FROM `" . $this->db->dbprefix . "user_permission` WHERE user_id = '" . (int)$user_id . "'");
		
		return true;
	}
	
	public function getUser($user_id) {
		$query = $this->db->query("SELECT * FROM `" . $this->db->dbprefix . "user` WHERE user_id = '" . (int)$user_id . "'");
	
		return $query->row_array();
	}
	
	public function getUserByCode($code) {
		$query = $this->db->query("SELECT * FROM `" . $this->db->dbprefix . "user` WHERE code = " . $this->db->escape($code) . " AND code != ''");
	
		return $query->row_array();
	}
		
	public function getUsers($data = array()) {
		$sql = "SELECT * FROM `" . $this->db->dbprefix . "user`";
			
		$sort_data = array(
			'username',
			'status',
			'date_added'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY username";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->result_array();
	}

	public function getTotalUsers() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . $this->db->dbprefix . "user`");
		
		return (int)$query->row('total');
	}

	public function getTotalUsersByGroupId($user_group_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . $this->db->dbprefix . "user` WHERE user_group_id = '" . (int)$user_group_id . "'");
		
		return (int)$query->row('total');
	}
	
	public function getTotalUsersByEmail($email) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . $this->db->dbprefix . "user` WHERE email = " . $this->db->escape($email) . "");
		
		return (int)$query->row('total');
	}	
}
?>
